﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using System.Net;

namespace Babelfish.Javascript.Tests
{
    public class Class1
    {
        [Fact]
        public void dkjashdkfjhfksdjh()
        {
            
            var nodes = JavascriptParser.Parse(new WebClient().DownloadString("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"));

            //var nodes = JavascriptParser.Parse(new WebClient().DownloadString("http://www.eniro.se/scripts/supersearch-core-nomin.js?2.10.13"));

        

            foreach (var node in nodes)
                PrintNode(node);
        }

        [Fact]
        public void TestObjectNotation()
        {
            var nodes = JavascriptParser.Parse(@"
                
                

                var b = 
                    {
                        Name:'this Is a name', 
                        number:564, 
                        innero:{ticks:10.5,faaa:function(){},a:fdsjkfkjsdhf},
                        f:function()
                        {
                            var apa = function()
                            {
                                var a,b,c;
                            }
                        }
                    }

                var a = ""'dfgdfg''"";
                var c = 'dfgdfg""';
               

                jQuery.fn = jQuery.prototype = 
                {
                    init:function(selector, context)
                    {
                        var a,b,c,d;


                        
                    }
                    ,p:[0,'jnkhjkh',3,5]
                    ,a:0
                    
                }

                var fdf = hfsdkfjhsdkjfh;


                var a = kjsdfj,b = dhfskdhfh,c = fjsdhfkjsfh;

            ");

            foreach (var node in nodes)
                PrintNode(node);
        }

        [Fact]
        public void klfdjsfklsdfj()
        {
            var nodes = JavascriptParser.Parse(@"

                var apa = ""kalle"";

                toString = Object.prototype.toString,
	                hasOwnProperty = Object.prototype.hasOwnProperty,
	                push = 'some test here, and here',
	                slice = ""Array.prototyp,,e.,,sl,,,,ice"",
	                indexOf = Array.prototype.indexOf;
            ");

            foreach (var node in nodes)
                PrintNode(node);
        }

        [Fact]
        public void lksdflfhsdfkldfh()
        {
            var nodes = JavascriptParser.Parse(@"

                function kalle()
                {
                    if(a == 0)
                    {
                        var p = 0;
                    }

                    while(a == 0)
                    {
                        var b = 0;
                    }

                    for(int i=0;i<apa;i++)
                    {
                        var c = 'hey it is a for loop';
                    
                        for(int i=0;i<apa;i++)
                        {
                            var d = 'hey it is a for loop';
                        }
                    }
                }
            ");

            foreach (var node in nodes)
                PrintNode(node);
        }


        [Fact]
        public void kdjshkfjh()
        {
            var nodes = JavascriptParser.Parse(@"

                var kalle = {pelle:olle, pelle:olle}

                var olle = function()
                {
                    while(true)
                    {

                    }
                }

                //jfgsdfhgsjh
                / *lfjkdlfkjsdflkdsjflkfj *  /

                function apa()
                {
                    var kalle = ""jkfhsdkjfhksdjh"";

                    var test = somethingelse?;

                    var olle = 561654654;

                    m = function()
                    {
                        what is this inside of m?
                    }

                    if(a == ""is a this?"")
                    {
                        alert(""foo"");
                    }

                    alert(""hello! alerting"");

                    //kjfhsdlfhsdfjsdfskjf
                }          
            ");


            foreach (var node in nodes)
                PrintNode(node);
        }

        private void PrintNode(JsObject o)
        {

            var parents = Parents(o);


            if (o.Type == JsObjectType.Comment)
                return;

            Console.WriteLine("----------------------------------------------------------------------");

            foreach (var s in parents.Reverse())
                Console.Write("{0} -> ", s);

            Console.WriteLine("{0}, {1}", o.Name, o.Type);


            foreach (var child in o.Children)
                PrintNode(child);
        }

        private IEnumerable<string> Parents(JsObject o)
        {
            if (o.Parent == null) 
                yield break;

            yield return o.Parent.Name;

            foreach (var s in Parents(o.Parent))
                yield return s;
        }
    }
}
